//
//  CouponManagerVC.m
//  GasBusiness
//
//  Created by zfc on 2021/8/1.
//

#import "CouponManagerVC.h"
#import "CouponManagerCell.h"
#import "CouponGiveVC.h"
@interface CouponManagerVC ()

@end

@implementation CouponManagerVC

- (void)viewDidLoad {
    [super viewDidLoad];

    self.navigationItem.title = @"优惠券管理";
    [self configTableView];
}

- (void) configTableView {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.leading.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view).offset(-kBottomWhiteHeight);
    }];
    [self.tableView registerClass:[CouponManagerCell class] forCellReuseIdentifier:@"CouponManagerCell"];

}

- (void)fetchData {
    NSString *url = @"/tiny-shop/v1/marketing/coupon-type/index";
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"page"] = @(self.page);
    [[NetWorksTools shared] requestWithMethod:GET url:url params:params successBlock:^(NSDictionary * _Nonnull response) {
        [self endRefresh];
        NSArray *arr = response[@"data"];
        if (self.page == 1) {
            [self.dataList removeAllObjects];
        } else {
            if (arr.count == 0) {
                self.page = self.page - 1;
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.dataList addObjectsFromArray:arr];
        [self.tableView reloadData];
    } failureBlock:^(NSError * _Nonnull error) {

    }];
}


#pragma mark tableView
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataList[indexPath.row];
    CouponManagerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponManagerCell"];
    WeakSelf;
    cell.givebtn.block = ^(UIButton *sender) {
        CouponGiveVC *vc = [[CouponGiveVC alloc] init];
        vc.dic = dic;
        [weakSelf.navigationController pushViewController:vc animated:YES];
    };
    cell.dic = dic;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 126;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ColorF5;
    return view;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = ColorF5;
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 5;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}



@end
